<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ]
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/1/10-15:46
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\admin\model;


    use app\admin\imple\Comm;
    use app\common\model\BaseModel;
    use think\Exception;

    class Roles extends BaseModel implements Comm
    {
        protected $name ="role";

        protected function initialize()
        {
            parent::initialize(); // TODO: Change the autogenerated stub
        }


        /**
         * @return \think\model\relation\HasMany
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/2/27
         * @name: adminUser
         * @describe:一对多
         */
        public function adminUser()
        {
            return $this->hasMany('AdminUser','role_id','id');
        }



        /**
         * @param array $param
         * @param string $order
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/10
         * @name: getRolePageData
         * @describe:权限列表
         */
        public function getAdminPageData($param=[],$order='id asc'){

            $where = [];
            if(!empty($param)) {
                //搜索条件
                if(!empty($param['name'])) {
                    $where[] = ['name','like', "%".$param['name']."%"];
                }
            }

            try{
                $data = self::with("adminUser")->where($where)->order($order)->page(PAGE)->limit(LIMIT)->select()->toArray();
                $count =self::where($where)->count("id");

                if (!empty($data)){
                    foreach ($data as $k=>$v){
                        if((int)$v['id']==1){  //超级管理
                            $data[$k]['rule_name'] = "All";
                        }else{
                            $ids = unserialize($v['rule']);

                            $ds  =\db("menu")->where(["id" => $ids])->column("title");

                            if(!empty($ds)) {
                                $caidan = implode(",", $ds);
                            }
                            else {
                                $caidan = "";
                            }
                            $data[$k]['rule_name'] = $caidan;
                        }
                    }
                }

                $this->ajaxResult($data,0,'',$count);

           }catch (Exception $exception){

               $this->ajaxResult('',100,$exception->getMessage());
            }

        }



        /**
         * @param $data
         * @return mixed|void
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/10
         * @name: addAndEdit
         * @describe:
         */
        public function addAndEdit($data)
        {
            // TODO: Implement addAndEdit() method.
           return parent::doAll($data);
        }



        /**
         * @param $id
         * @return mixed|void
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/13
         * @name: del
         * @describe:
         */
        public function del($id)
        {
            // TODO: Implement del() method.
            return parent::JrkDelete($id);
        }





    }